import QtQuick 1.0
import "Dialogs"
import "ListViews"
import "Models"
import "WebAccess/TwitterAPI.js" as TwitterAPI
import "Util/Util.js" as Util


Item {
    id: _root
    anchors.fill: parent
    property real density: 1.0

    function open(name){
        _root.state = name;

    }

    function close(){
        _root.state = "";
        _rootContainer.setFocusTextEdit(false);
    }

    /////////////////////////////////////////////
    // 現在表示されてるダイアログのオブジェクトを取得する
    function getActiveDialogObject(){
        var dlg = null;

        switch(state){
        case "Auth":
            dlg = _dialogAuth;
            break;
        case "Setting":
            dlg = _dialogSetting;
            break;
        case "RetweetCaution":
            dlg = _dialogRetweetCaution;
            break;
        case "FavoriteCaution":
            dlg = _dialogFavoriteCaution;
            break;
        case "FollowCaution":
            dlg = _dialogFollowCaution;
            break;
        case "ExitCaution":
            dlg = _dialogExitCaution;
            break;
        case "AboutCaution":
            dlg = _dialogAbout;
            break;
        case "Help":
            dlg = _dialogHelp;
            break;
        default:
            break;
        }
        return dlg;
    }

    MouseArea {
        id: _blankArea
        x: 0
        y: 0
        width: 0
        height: 0
        onClicked: {
        }
    }

    // キーイベント
    function onKeyPressed(event){
        switch(event.key){
        case Qt.Key_Escape:
            close(true);
            event.accepted = true;
            break;
        default:
            break;
        }
    }

    //状態が変わった時の処理
    onStateChanged: {
        var dlg = getActiveDialogObject();
        if(dlg === null){
            // 消えた
            _rootContainer.setFocusTextEdit(false);
        }else{
            //dlg.setForceActiveFocus();
            dlg.viewEvent();
        }
    }

    /////////////////////////////////////
    //Auth認証のダイアログ
    DialogAuthorization {
        id: _dialogAuth
//        width: 200
//        height: 260
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        opacity: 0
        density: _root.density

        onClosePressed: {
            close();
        }
    }

    /////////////////////////////////////
    //設定のダイアログ
    DialogSetting {
        id: _dialogSetting
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        opacity: 0
        density: _root.density

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }


    //////////////////////////////////
    //Retweetの確認
    DialogMessage {
        id: _dialogRetweetCaution
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Retweet"
        dialogMessage: "Do you do ReTweet of a remark of " + _selectItemInfo.screenName

        //OKボタンイベント
        onClickOkButton: {
            if(_selectItemInfo.itemId.length !== 0){
                var obj = new TwitterAPI.sendRetweet(_selectItemInfo.itemId, onRetweetRequest);
            }
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }
    function onRetweetRequest(text){
        _toast.show("Did Retweet")
        _selectItemInfo.clear();
    }


    //////////////////////////////////
    //Favoriteの確認
    DialogMessage {
        id: _dialogFavoriteCaution
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Favorite"
        dialogMessage: _selectItemInfo.isFavorite ?
                           "Do you remove a favorite?"
                         : "Do you register a favorite?"

        //OKボタンイベント
        onClickOkButton: {
            var obj = new TwitterAPI.sendFavoriteRequest(_selectItemInfo.itemId
                                           , !_selectItemInfo.isFavorite
                                           , onFavoriteRequest );
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }
    function onFavoriteRequest(text){
        if(_selectItemInfo.isFavorite){
            // 解除
            _toast.show("Removed a favorite.")
        }else{
            // 登録
            _toast.show("Register a favorite.")
        }
        _selectItemInfo.clear();
    }

    //////////////////////////////////
    //Followの確認
    DialogMessage {
        id: _dialogFollowCaution
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Friendship"
        dialogMessage: _selectItemInfo.isFollow ?
                           "Do you remove friendship?"
                         : "Do you request friendship?"

        //OKボタンイベント
        onClickOkButton: {
            var obj = new TwitterAPI.sendFollowRequest(_selectItemInfo.screenName
                                           , !_selectItemInfo.isFollow
                                           , onFollowRequest );
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }
    function onFollowRequest(text){
        if(_selectItemInfo.isFollow){
            // 解除
            _toast.show("Removed frinedship.")
        }else{
            // 登録
            _toast.show("Requested friendship.")
        }
        _selectItemInfo.clear();
    }

    ///////////////////////////////////
    //終了確認
    DialogMessage {
        id: _dialogExitCaution
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Attention"
        dialogMessage: "Exit ?"

        //OKボタンイベント
        onClickOkButton: {
            Qt.quit();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    /////////////////////////////////////
    //バージョン情報
    DialogMessage {
        id: _dialogAbout
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "About"
        dialogMessage: "Rewit for Qt  Version " + _Setting.versionNumber
                       + "\nCopyright (C) 2011 IoriAYANE"

        visibleCancel: false

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    /////////////////////////////////////
    //ヘルプ
    DialogMessage {
        id: _dialogHelp
        opacity: 0
        x: (_root.width - width) / 2
        y: (_root.height - height) / 2
        density: _root.density

        dialogTitle: "Help"
        dialogMessage: "Key bind\n"
                       + " - Ctrl+Enter : tweet\n"
                       + " - Enter : select or open context menu\n"
                       + "\n"
                       + " - Tab : move focus\n"
                       + " - Esc : close tweet area\n"
                       + " - F5 : update\n"
                       + "\n"
                       + " - Home : move to top item\n"
                       + " - End : move to bottom item\n"
                       + " - BackSpace : back to Home\n"
                       + " - Cursor Up/Down : move cursor\n"
                       + " - Cursor Left : back to Home\n"
        visibleCancel: false

        //OKボタンイベント
        onClickOkButton: {
            close();
        }
        //キャンセルボタンイベント
        onClickCancelButton: {
            close();
        }
    }

    states: [
        State {
            name: "Auth"
            PropertyChanges{
                target: _dialogAuth
                opacity: 1.0
//                y: _root.height * 0.2
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "Setting"
            PropertyChanges{
                target: _dialogSetting
                opacity: 1.0
//                y: _root.height * 0.2
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "RetweetCaution"
            PropertyChanges{
                target: _dialogRetweetCaution
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "FavoriteCaution"
            PropertyChanges{
                target: _dialogFavoriteCaution
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "FollowCaution"
            PropertyChanges{
                target: _dialogFollowCaution
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "ExitCaution"
            PropertyChanges{
                target: _dialogExitCaution
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "AboutCaution"
            PropertyChanges{
                target: _dialogAbout
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        },
        State {
            name: "Help"
            PropertyChanges{
                target: _dialogHelp
                opacity: 1.0
            }
            PropertyChanges {
                target: _blankArea
                width: _root.width
                height: _root.height
            }
        }
    ]
    transitions: Transition {
        NumberAnimation {
            easing.type: Easing.OutCubic
            targets: [
                     _dialogAuth, _dialogSetting
                    , _dialogRetweetCaution, _dialogFavoriteCaution, _dialogFollowCaution
                    , _dialogExitCaution
                    , _dialogAbout, _dialogHelp]
            properties: "opacity"
            duration: 500
        }
    }

}
